<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>客户资料</title>
    <link rel="stylesheet" href="../../../../public/dist/css/layui.css">
</head>

<body>

    <form class="layui-form" action="" id="example" lay-filter="example">
        <div class="select common">
            <div class="select-title"> 客户资料</div>
            <div class="select-con">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间:</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tesss" class="layui-input input-d   ming" name="createTime" placeholder="输入时间">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户简称:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d   mingming" name="shortName" placeholder="输入客户简称">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户全称:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  house" placeholder="输入客户全称" name="fullName">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">平台账号:</label>
                        <div class="layui-input-inline" id="hether">
                            <input type="text" class="layui-input input-d  hao" placeholder="输入平台账号" name="webId">
                        </div>
                    </div>
                </div>



                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">国家:</label>
                        <div class="layui-input-inline ">
                            <select name="country" lay-filter="country" class="superior">
                                        <option value="">---请选择---</option>
                                        <option value="0">中国</option>
                                 </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">省:</label>
                        <div class="layui-input-inline ">
                            <select name="state" lay-filter="sheng" class="shseng">
                                        <option value="">---请选择---</option>
                                        
                                 </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">城市:</label>
                        <div class="layui-input-inline ">
                            <select name="city" lay-filter="cshi" class="cshi">
                                        <option value="">---请选择---</option>
                                        
                                 </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">详细地址:</label>
                        <div class="layui-input-inline">
                            <textarea type="text" class="layui-input input-d  xx" placeholder="输入详细地址" name="custAdd"></textarea>
                        </div>
                    </div>
                </div>



                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">联系人:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  liang" placeholder="输入联系人" name="principal">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机:</label>
                        <div class="layui-input-inline">
                            <input type="number" class="layui-input input-d  shou" placeholder="输入手机" name="mobNo" maxlength="11">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户类型:</label>
                        <div class="layui-input-inline ">
                            <select name="customerCodeName" lay-filter="nigs" class="layui-form nigs">
                                        <option value="">---请选择---</option>
                                        
                                 </select>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户类型代号:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  daihao" placeholder="输入客户类型代号" name="customerCode">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务部门:</label>
                        <div class="layui-input-inline ">
                            <select name="salesdept" lay-filter="salesdept" class="salesdept">
                                        <option value="">---请选择---</option>
                                        
                                 </select>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务员名称:</label>
                        <div class="layui-input-inline ">
                            <select name="salesmanName" lay-filter="salesmanName" class="salesmanName">
                                        <option value="">---请选择---</option>
                                        
                                 </select>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务员代号:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  salesman " placeholder="输入业务员代号" name="salesman" disabled>
                        </div>
                    </div>
                </div>



                <div class="layui-form-item  xiug">
                    <div class="layui-inline">
                        <label class="layui-form-label">修改时间:</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tesss1" class="layui-input input-d  xiu" placeholder="输入时间">
                        </div>
                    </div>
                </div>

                <div class="select-btn" style="margin-left:30px;">
                    <div class="layui-bg-green comquery queding" id="queding">确定</div>
                    <div class="layui-bg-green comquery cancels">取消</div>
                </div>
            </div>
        </div>
    </form>

</body>
<script src="../../../../js/module/jquery-3.4.1.min.js"></script>
<script src="../../../../public/dist/layui.js" charset="utf-8"></script>
<script src="../../../../js/common/api.js"></script>

</html>


<script>
    layui.use(["table", "laydate", "form"], function() {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            obj = {},
            coo = null,
            table = layui.table;

        //时间
        laydate.render({
            elem: '#tesss'
        });

        //时间
        laydate.render({
            elem: '#tesss1'
        });


        //取消
        $(".cancels").click(function() {
            window.location.href = "../customerdata.html";
        });

        //客户类型名称
        noAjax("coa/getCustomerTypeList", {}, "get").done(function(resp) {
            console.log(resp)
            $.each(resp.ipage.records, function(v, item) {
                $(".nigs").append('<option  value="' + item.customerCode + '">' + item.name + '</option>')
            })
            coo = resp.ipage.records;
            form.render('select', "nigs");
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(nigs)', function(data) {
            obj.customerCodeName = $(this)[0].innerText;
            $(".daihao").val(data.value);
        });



        obj.country = "";
        obj.city = "";
        obj.state = "";
        //从国家中选择出省
        form.on('select(country)', function(data) {

            obj.country = $(this)[0].innerText;
            noAjax("enterprise/getProvincecity", {
                parentid: data.value
            }, "get").done(function(resp) {
                $(".shseng").children().remove();
                $(".shseng").append("<option>---请选择---</option>");
                $.each(resp.list, function(v, item) {
                    $(".shseng").append('<option  value="' + item.code + '">' + item.fullName + '</option>');
                })
                form.render('select');
                return
            }).fail(function(err) {
                console.log(err)
            });
            return
        });

        // 市
        form.on('select(cshi)', function(data) {
            obj.city = $(this)[0].innerText;
            return
        });

        //选择省
        form.on('select(sheng)', function(data) {
            obj.state = $(this)[0].innerText;
            noAjax("enterprise/getProvincecity", {
                parentid: data.value
            }, "get").done(function(resp) {
                console.log(resp)
                $(".cshi").children().remove();
                $(".cshi").append("<option>---请选择---</option>");
                $.each(resp.list, function(v, item) {
                    $(".cshi").append('<option  value="' + item.code + '">' + item.fullName + '</option>');
                })
                form.render('select');
                return
            }).fail(function(err) {
                console.log(err)
            });
            return
        });


        //业务部门
        noAjax("coa/getDeptList", {}, "get").done(function(resp) {
            $.each(resp.ipage.records, function(v, item) {
                $(".salesdept").append('<option  value="' + item.description + '">' + item.description + '</option>')
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(salesdept)', function(data) {
            obj.salesdept = data.value

        });


        //业务员名称
        noAjax("coa/getEmployeeList", {}, "get").done(function(resp) {
            console.log(resp)
            $.each(resp.ipage.records, function(v, item) {
                $(".salesmanName").append('<option  value="' + item.employeeId + '">' + item.name + '</option>')
            })

            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });
        form.on('select(salesmanName)', function(data) {
            obj.salesman = data.value;
            obj.salesmanName = $(this)[0].innerText;
            $(".salesman").val(data.value);
        });



        //编辑
        var urls = window.location.search;
        urls = urls.substring(4);
        noAjax("coa/getCustomerList", {
            id: urls
        }, "get").done(function(resp) {

            if (urls != "") {
                $(".xiug").show();
                $("#tesss").attr("disabled", true);
                resp.ipage.records[0].createTime = resp.ipage.records[0].createTime.substring(0, 10);

                var selects = 'dd[lay-value=' + resp.ipage.records[0].customerCode + ']';
                console.log(selects)
                console.log($(".nigs").siblings("div.layui-form-select").find('dl').find(selects))
                $(".nigs").siblings("div.layui-form-select").find('dl').find(selects).click();

            } else {
                $(".xiug").hide();
            }
            console.log(resp.ipage.records);
            form.val("example", resp.ipage.records[0]);

            return
        }).fail(function(err) {
            console.log(err)
        });

        //手机号
        $(".shou").blur(function() {
            if (!(/^1[3456789]\d{9}$/.test($(this).val()))) {
                alerts("请输入正确手机号");
                $(this).val("");
                return false;
            }
        })

        //确定
        $("#queding").click(function() {
            if ($("#tesss").val() == "") {
                alerts("请选择创建时间");
                return
            }

            if ($(".mingming").val() == "") {
                alerts("请输入客户简称");
                return
            }
            if ($(".house").val() == "") {
                alerts("请输入客户全称");
                return
            }

            if ($(".superior").val() == "") {
                alerts("请选择国家");
                return
            }
            if ($(".shseng").val() == "") {
                alerts("请选择省");
                return
            }
            if ($(".cshi").val() == "") {
                alerts("请选择城市");
                return
            }
            if ($(".xx").val() == "") {
                alerts("请输入详细地址");
                return
            }
            if ($(".liang").val() == "") {
                alerts("请输入联系人");
                return
            }

            if ($(".shou").val() == "") {
                alerts("请输入手机号");
                return
            }

            if ($(".nigs").val() == "") {
                alerts("请选择客户类型");
                return
            }
            if ($(".salesmanName").val() == "") {
                alerts("请选择业务员名称");
                return
            }
            if ($(".salesdept").val() == "") {
                alerts("请选择部门");
                return
            }

            obj.shortName = $(".mingming").val();
            obj.fullName = $(".house").val();
            obj.webId = $(".hao").val();
            obj.createTime = $("#tesss").val() + " " + "00:00:00";
            obj.custAdd = $(".xx").val();
            obj.customerCode = $(".daihao").val();
            obj.mobNo = $(".shou").val();
            obj.principal = $(".liang").val();


            if (urls != "") {
                obj.id = urls;
                obj.editTime = $("#tesss1").val() + " " + "00:00:00";
                console.log(obj);

                handleAjax("coa/updateCustomer", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../customerdata.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });

            } else {
                obj.editTime = "";

                handleAjax("coa/addCustomer", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../customerdata.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });
            }
        });
    });
</script>